'''

序列化：把一个对象变成字符串（str）或者字节串（bytes）
反序列化：把字符串或者字节串还原成对象
json - dumps / loads - 字符串
pickle - dumps / loads -字节串
'''
import json
import pickle
import redis
class Student:
    def __init__(self,stuname,stuid,stugender,stuaddr):
        self.stuname = stuname
        self.stuid = stuid
        self.stugender = stugender
        self.stuaddr = stuaddr
    def __repr__(self):
        return '%d: %s' % (self.stuid,self.stuname)


class StudentJsonEncoder(json.JSONEncoder):
    def default(self, o):
        return o.__dict__



stu1 = Student('胡一刀',1001,'Ture','四川')
stu2 = Student('小米', 1002, 'Ture', '广东')
student = [stu1, stu2]
# pickle序列化
print(pickle.dumps(student))

redis_cli = redis.Redis(host='47.107.87.249', port=6379, password='1234')
if redis_cli.ping():
    # 添加到Redis中
    # redis_cli.set('students',pickle.dumps(student))
    # pickle反序列化
    students = pickle.loads(redis_cli.get('students'))
    print(students)
else:
    print('连接失败')






